#include "stm32f10x.h"
int16_t Encoder_Count1,Encoder_Count2=50,Encoder_Count3=170,Encoder_Count4,B_level, Cnt;

void Encoder_Init(void)
{
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);

	RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);	
	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
	GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_4| GPIO_Pin_3| GPIO_Pin_6| GPIO_Pin_5|GPIO_Pin_7;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA, &GPIO_InitStructure);
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_1|GPIO_Pin_0;
	GPIO_Init(GPIOB, &GPIO_InitStructure);

	GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource10);
   GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource4);	
   GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource6);	
   GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource0);		
	EXTI_InitTypeDef EXTI_InitStructure;
	EXTI_InitStructure.EXTI_Line = EXTI_Line10;
	EXTI_InitStructure.EXTI_LineCmd = ENABLE;
	EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
	EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising_Falling;
	EXTI_Init(&EXTI_InitStructure);
	
	EXTI_InitStructure.EXTI_Line = EXTI_Line4;
	EXTI_InitStructure.EXTI_LineCmd = ENABLE;
	EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
	EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising_Falling;
	EXTI_Init(&EXTI_InitStructure);
	
	EXTI_InitStructure.EXTI_Line = EXTI_Line6;
	EXTI_InitStructure.EXTI_LineCmd = ENABLE;
	EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
	EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising_Falling;
	EXTI_Init(&EXTI_InitStructure);
	
	EXTI_InitStructure.EXTI_Line = EXTI_Line0;
	EXTI_InitStructure.EXTI_LineCmd = ENABLE;
	EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
	EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising_Falling;
	EXTI_Init(&EXTI_InitStructure);
	
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);	
	NVIC_InitTypeDef NVIC_InitStructure;
	NVIC_InitStructure.NVIC_IRQChannel = EXTI15_10_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
	NVIC_Init(&NVIC_InitStructure);
	
	NVIC_InitStructure.NVIC_IRQChannel = EXTI4_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
	NVIC_Init(&NVIC_InitStructure);
	
	NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
	NVIC_Init(&NVIC_InitStructure);
	
	NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
	NVIC_Init(&NVIC_InitStructure);
}

int16_t Encoder_Get1(void)
{
	int16_t Temp;
	Temp = Encoder_Count1;
	Encoder_Count1 = 0;
	return Temp;
}

int16_t Encoder_Get2(void)
{
	int16_t Temp;
	Temp = Encoder_Count2;
	Encoder_Count2 = 0;
	return Temp;
}

int16_t Encoder_Get3(void)
{
	int16_t Temp;
	Temp = Encoder_Count3;
	Encoder_Count3 = 0;
	return Temp;
}

int16_t Encoder_Get4(void)
{
	int16_t Temp;
	Temp = Encoder_Count4;
	Encoder_Count4 = 0;
	return Temp;
}


void EXTI4_IRQHandler(void)
{
	if(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_4) == 0 && Cnt==0) //A相下降沿触发第一次中断
	{
		Cnt++;
		B_level=0;
		if(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_3) == 1)  //读取B相电平
		{
			B_level=1;
		}
	}
	if(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_4) == 1 && Cnt==1) //A相上升沿触发第二次中断
	{
      Cnt=0;
		if(B_level==1 && GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_3) == 0) //正转时A相下降沿B相高电平，A相上升时B相低电平
		{
			Encoder_Count1-=2;
		}
		if(B_level==0 && GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_3) == 1)  //反转相反
		{
			Encoder_Count1+=2;
		}	 
	}	
	EXTI_ClearITPendingBit(EXTI_Line4);	
}


void EXTI9_5_IRQHandler(void)
{
	if(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_6) == 0 && Cnt==0)
	{
		Cnt++;
		B_level=0;
		if(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_5) == 1)
		{
			B_level=1;
		}
	}
	if(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_6) == 1 && Cnt==1)
	{
        Cnt=0;
		if(B_level==1 && GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_5) == 0) 
		{
			Encoder_Count2-=2;
		}
		if(B_level==0 && GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_5) == 1)
		{
			Encoder_Count2+=2;
		}	 
	}	
	EXTI_ClearITPendingBit(EXTI_Line6);	
}


void EXTI0_IRQHandler(void)
{
	if(GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_0) == 0 && Cnt==0)
	{
		Cnt++;
		B_level=0;
		if(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_7) == 1)
		{
			B_level=1;
		}
	}
	if(GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_0) == 1 && Cnt==1)
	{
      Cnt=0;
		if(B_level==1 && GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_7) == 0) 
		{
			Encoder_Count3+=2;
		}
		if(B_level==0 && GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_7) == 1)
		{
			Encoder_Count3-=2;
		}	 
	}	
	EXTI_ClearITPendingBit(EXTI_Line0);	
}


void EXTI15_10_IRQHandler(void)
{
	if(GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_10) == 0 && Cnt==0)
	{
		Cnt++;
		B_level=0;
		if(GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_1) == 1)
		{
			B_level=1;
		}
	}
	if(GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_10) == 1 && Cnt==1)
	{
        Cnt=0;
		if(B_level==1 && GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_1) == 0) 
		{
			Encoder_Count4+=2;
		}
		if(B_level==0 && GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_1) == 1)
		{
			Encoder_Count4-=2;
		}	 
	}	
	EXTI_ClearITPendingBit(EXTI_Line10);	
}






